package com.kingyea.mobilepolice.apimanage.log.service.impl;

import com.kingyea.mobilepolice.apimanage.log.bean.OperationLog;
import com.kingyea.mobilepolice.apimanage.log.dao.OperationLogMapper;
import com.kingyea.mobilepolice.apimanage.log.service.OperationLogService;
import com.kingyea.mobilepolice.apimanage.systemsync.Send2Queue;
import com.kingyea.mobilepolice.apimanage.utils.page.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * @author Mr.Lin
 * @version v1.0.0
 * @description OperationLog 实现类
 * @date Created in 2018-02-27 16:20:46
 */
@Service
@Transactional
public class OperationLogServiceImpl implements OperationLogService {

    private static final Logger LOGGER = LoggerFactory.getLogger(OperationLogServiceImpl.class);

    @Resource
    private OperationLogMapper operationLogMapper;

    @Value("${server.regId}")
    private String regId;

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @Override
    public OperationLog getById(Long id) throws Exception {
        return this.operationLogMapper.getById(id);
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public List<OperationLog> list(OperationLog operationLog) throws Exception {
        List<OperationLog> list = operationLogMapper.list(operationLog);
        return list;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Send2Queue
    @Override
    public int deleteById(Long id) throws Exception {
        operationLogMapper.deleteById(id);
        return 1;
    }

    /**
     * 保存
     *
     * @param operationLog
     * @return
     */
    @Send2Queue
    @Override
    public OperationLog save(OperationLog operationLog) throws Exception {
        operationLog.setRegId(regId);
        operationLogMapper.save(operationLog);
        return operationLog;
    }

    @Async
    @Override
    public Future<OperationLog> asyncSave(OperationLog operationLog) throws Exception {
        try {
            return new AsyncResult<>(save(operationLog));
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage());
            return new AsyncResult<>(null);
        }
    }

    /**
     * 更新
     *
     * @param operationLog
     * @return
     */
    @Send2Queue
    @Override
    public OperationLog update(OperationLog operationLog) throws Exception {
        operationLogMapper.update(operationLog);
        return operationLog;
    }

    @Override
    public Object searchByCondition(Map<String, Object> map) throws Exception {
        PageInfo.startPage(map);
        return PageInfo.getPageInfo(operationLogMapper.searchByCondition(map));
    }


}
